$(document.body).css({
    "overflow-x": "hidden",
    "overflow-y": "hidden"
});

// 禁止页面滚动ios出白屏
document.body.addEventListener('touchmove', function (e) {
    e.preventDefault();
}, { passive: false });

// 首页
function part1() {
    $(".icon_1_1").addClass("icon_1_1_1");
    setTimeout(_ => {
        $(".txt1").fadeOut(500, _ => {
            $(".txt2").fadeIn(1500, _ => {
                setTimeout(_ => {
                    $(".txt2").fadeOut(500, _ => {
                        $(".txt3").fadeIn(1500);
                        $('.icon_1_1').hide();
                        $('.icon_1_3').show();
                        $('.icon_1_5').show();
                        scrollUp("start", _ => {
                            // 判断是跳转引导页还是游戏页
                            if (localStorage.getItem('subscribe') == 1) {
                                // 游戏页
                                jumpPage($(".start"), $(".page2"), part3);
                            } else {
                                // 关注页
                                jumpPage($(".start"), $(".page1"));
                            }
                        })
                    })
                }, 3000)
            })
        })
    }, 4000)
}

// 游戏页
function part3() {
    $(".pic_3_7").addClass("pic_3_7_no_click")
    $(".page2").one("click", _ => {
        setTimeout(_ => {
            $(".pic_3_4").show();
        }, 1300)
        $(".pic_3_7").removeClass("pic_3_7_no_click")
        let timeCount = 6;
        $(".cout-down-txt").html(timeCount + ' s').show();
        $(".pic_3_2").addClass("pic_3_2_1");
        var timeInterval = setInterval(_ => {
            $(".cout-down-txt").html(--timeCount + ' s');
            if (timeCount === 0) {
                clearInterval(timeInterval)
                $(".pic_3_9").show();
                $(".pic_3_6").show();
                $(".pic_3_8").hide();
                $(".pic_3_7").hide();
                // 判断用户时候有openid,如果没有说明没授权，不能进入抽奖规避重复抽奖风险
                let openid = localStorage.getItem('openid');
                if (!openid) {
                    reloadPage('您未授权，请授权页面再试');
                } else {
                    scrollUp("page2", _ => {
                        let prize = parseInt(localStorage.getItem('prize'));
                        if (prize) {
                            // 抽过奖跳到反馈页
                            jumpPage($(".page2"), $(".page_end"), part5(prize));
                        } else {
                            // 没抽过奖跳到抽奖页
                            jumpPage($(".page2"), $(".page3"), part4);
                        }
                    })
                }
            }
        }, 1000)
    })
};

function scrollUp(domClass, callBack) {
    let startY = 0;
    let flag = true;
    document.getElementsByClassName(domClass)[0].addEventListener('touchstart', function (e) {
        startY = e.touches[0].pageY;
    }, { passive: false });

    document.getElementsByClassName(domClass)[0].addEventListener('touchmove', function (e) {
        let endY = e.touches[0].pageY;
        if (endY < startY && flag) {
            flag = false;
            callBack && callBack()
        }
    }, { passive: false });
}

// 反馈页
function part5(prize) {
    // 结果页显示
    $(".page_end").addClass(`page4_${prize}`)
    $(`.ending${prize}`).show();
};

// 抽奖页TODO
function part4() {
    let random = Math.floor(Math.random() * 4) + 1;
    // 奖品显示
    $(`.result${random}`).show();
    // 保存抽奖结果
    let params = {
        openid: localStorage.getItem('openid'),
        prize: random
    };
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=UTF-8",
        url: "http://sh20200720.hantieyi.cn/api/setPrize",
        data: JSON.stringify(params),
        success: (res) => {
            if (res.status == 200) {
                console.log('保存成功');
            } else {
                console.log('服务错误，保存失败');
            }
        },
        //请求失败，包含具体的错误信息
        error: (e) => {
            console.log(e);
        }
    });
}

// 页面跳转
function jumpPage(pageBack, pageNext, callBack) {
    pageNext.show().css("opacity", '0');
    setTimeout(() => {
        pageNext.css("opacity", '1')
        pageBack.hide();
        callBack && callBack();
    }, 500);
}

// 重新加载页面
function reloadPage(res) {
    alert(res);
    window.location.reload();
}

//图片预加载
var num2 = 0;
var the_images = [
    "images/loading.gif",
    "images/pic_3_4.gif",
    "images/bg1_2.jpg",
    "images/bg2.jpg",
    "images/bg3.png",
    "images/bg4.jpg",
    "images/bg5.png",
    "images/bg6.png",
    "images/bg7.png",
    "images/bg8.png",
    "images/icon_1_1.png",
    "images/icon_1_2.png",
    "images/icon_1_3.png",
    "images/icon_1_4.png",
    "images/icon_1_5.png",
    "images/logo.png",
    "images/pic_2_1.png",
    "images/pic_2_2.png",
    "images/pic_2_3.png",
    "images/pic_3_1.png",
    "images/pic_3_2.png",
    "images/pic_3_3.png",
    "images/pic_3_5.png",
    "images/pic_3_6.png",
    "images/pic_3_7.png",
    "images/pic_3_8.png",
    "images/pic_3_9.png",
    "images/pic_3_10.png",
    "images/pic_4_1.png",
    "images/pic_4_2.png",
    "images/pic_4_3.png",
    "images/pic_4_4.png",
    "images/pic_5_1.png",
    "images/pic_5_2.png",
    "images/pic_5_3.png",
    "images/pic_5_4.png",
    "images/pic_5_5.png",
    "images/txt_1.png",
    "images/txt_2.png",
    "images/txt_3.png"
];
jQuery.imgpreload(the_images, {
    each: function () {
        var status = $(this).data('loaded') ? 'success' : 'error';
        if (status == "success") {
            ++num2;
            $("#lodingnum").html((num2 / the_images.length * 100).toFixed(0) + "%");
        }
    },
    all: function () {
        $("#lodingnum").html("100%");
        setTimeout(function () {
            document.getElementById('loading').style.display = "none";
            $("#loading").hide(_ => {
                part1()
            })
        }, 300)
    }
})



